Before we begin, we must clear two concepts: directory and path, to understand Bash commands better.
A directory is a folder in any operating system. We use directories to store files and other directories.
A path specifies the location of a directory or a file. For example:
/home/poridhi/Desktop
It means that to find the Desktop directory, we have to go through home, then poridhi, and finally we will find Desktop.
Absolute Path: Starts from the root directory of the Linux filesystem. The command pwd (print working directory) prints the absolute path of a directory.
poridhi@ubuntu:~$ pwd
/home/poridhi
Relative Path: The location of a directory or a file relative to the current working directory. Example:
poridhi@ubuntu:~/bash$ ls
Image
poridhi@ubuntu:~/bash$ cd Image
poridhi@ubuntu:~/bash/Image$ pwd
/home/poridhi/bash/Image
cd (Change Directory)Used to change the current working directory.
Examples:
poridhi@ubuntu:~$ cd /home/poridhi/Desktop
poridhi@ubuntu:~/Desktop$
poridhi@ubuntu:~/Desktop$ cd ..
poridhi@ubuntu:/home/poridhi$
poridhi@ubuntu:/home/poridhi$ cd ~
poridhi@ubuntu:~$
poridhi@ubuntu:~$ cd /tmp
poridhi@ubuntu:/tmp$
ls (List Directory Contents)Lists the contents (files and folders) of a directory.
Examples:
poridhi@ubuntu:~$ ls
Desktop Documents Downloads
poridhi@ubuntu:~$ ls -a
. .. .bashrc Desktop Documents
poridhi@ubuntu:~$ ls -al
drwxr-xr-x 2 poridhi poridhi 4096 Jan 1 10:00 Desktop
-rw-r--r-- 1 poridhi poridhi 220 Jan 1 10:00 .bashrc
mkdir (Make Directory)Creates a new directory.
Examples:
poridhi@ubuntu:~$ mkdir new_folder
poridhi@ubuntu:~$ ls
new_folder
rmdir (Remove Directory)Removes an empty directory.
Examples:
poridhi@ubuntu:~$ ls
new_folder
poridhi@ubuntu:~$ rmdir new_folder
poridhi@ubuntu:~$ ls
rm (Remove)Used to delete files or directories.
Examples:
# Remove a file
poridhi@ubuntu:~/Desktop$ rm file.txt
# Remove a directory and its contents
poridhi@ubuntu:~/Desktop$ rm -r old_folder
# Prompt before deleting
poridhi@ubuntu:~/Desktop$ rm -i file.txt
touchCreates an empty file.
Examples:
poridhi@ubuntu:~/Desktop$ touch new_file.txt
poridhi@ubuntu:~/Desktop$ ls
new_file.txt
# Create multiple files
poridhi@ubuntu:~/Desktop$ touch file1.txt file2.txt
poridhi@ubuntu:~/Desktop$ ls
file1.txt file2.txt
cp (Copy)Copies files or directories.
Examples:
# Copy a file
poridhi@ubuntu:~/Desktop$ cp file1.txt /home/poridhi/Documents/
# Copy a directory
poridhi@ubuntu:~/Desktop$ cp -r folder1 /home/poridhi/Documents/
# Copy and preserve attributes
poridhi@ubuntu:~/Desktop$ cp -p file1.txt /home/poridhi/Documents/
mv (Move)Moves or renames files and directories.
Examples:
# Move a file
poridhi@ubuntu:~/Desktop$ mv file1.txt /home/poridhi/Documents/
# Rename a file
poridhi@ubuntu:~/Desktop$ mv file1.txt file2.txt
# Move multiple files
poridhi@ubuntu:~/Desktop$ mv file1.txt file2.txt /home/poridhi/Documents/
cat (Concatenate)Displays or combines files.
Examples:
# View a file
poridhi@ubuntu:~/Desktop$ cat file1.txt
these are the contents of file1.txt
# Combine files (> overwrite the content of combined.txt)
poridhi@ubuntu:~/Desktop$ cat file1.txt file2.txt > combined.txt
poridhi@ubuntu:~/Desktop$ cat combined.txt
these are the contents of file1.txt
and these are of file2.txt combined in combined.txt
# Append a file (>> appended so combined.txt's contents will stay)
poridhi@ubuntu:~/Desktop$ cat file1.txt >> combined.txt
poridhi@ubuntu:~/Desktop$ cat combined.txt
these are the contents of combined.txt then
contents of file1.txt added after combined.txt contents
findSearches for files or directories.
Examples:
# Find all .txt files in the current directory
poridhi@ubuntu:~/Desktop$ find . -name "*.txt"
./dir1/file2.txt
./dir1/file1.txt
./dir1/file3.txt
./dir1/file4.txt
./file3.txt
./file4.txt
# Find a specific file
poridhi@ubuntu:~/Desktop$ find /home/poridhi -name "file1.txt"
./dir1/file1.txt
# Find by size, larger than 10MB
poridhi@ubuntu:~/Desktop$ find / -size +10M
/boot/vmlinuz-6.8.0-51-generic
/boot/initrd.img-6.8.0-50-generic
/boot/vmlinuz-6.8.0-50-generic
/boot/initrd.img-6.8.0-51-generic
# Exclude certain directories
poridhi@ubuntu:~/Desktop$ find . -name "*.log" -not -path "./backup/*"
grepSearches for patterns in files.
Examples:
cat hello.txt
hello world
this is a word
this is a text to make grep simple
everything is simple
say hello to everyone
# Case-sensitive search
poridhi@ubuntu:~/Desktop$ grep "hel" hello.txt
hello world
say hello to everyone
poridhi@ubuntu:~/Desktop$ grep "every" hello.txt
everything is simple
say hello to everyone
# Case-insensitive search with line numbers
poridhi@ubuntu:~/Desktop$ grep -ni "GRe" hello.txt
2. this is a text to make grep example
# Search in multiple files
poridhi@ubuntu:~/Desktop$ grep "error" *.txt
# Search for a whole word only
poridhi@ubuntu:~/Desktop$ grep -w "every" hello.txt
poridhi@ubuntu:~/Desktop$ grep -w "everyone" hello.txt
say hello to everyone
headDisplays the first few lines of a file.
Examples:
# Default first 10 lines
poridhi@ubuntu:~/Desktop$ head hello.txt
hello world
say hello to everyone
.
.
.
# Custom number of lines, first 2 lines
poridhi@ubuntu:~/Desktop$ head -n 2 hello.txt
hello world
say hello to everyone
tailDisplays the last few lines of a file.
Examples:
# Default last 10 lines
poridhi@ubuntu:~/Desktop$ tail hello.txt
.
.
.
everything is simple
say hello to everyone
# Custom number of lines, 2 lines
poridhi@ubuntu:~/Desktop$ tail -n 2 hello.txt
everything is simple
say hello to everyone
chmodChanges file or directory permissions.
Examples:
# Add execute permission for everyone
poridhi@ubuntu:~/Desktop$ chmod +x script.sh
# Change to full permissions for owner, group, and others
poridhi@ubuntu:~/Desktop$ chmod 777 file.txt
# Remove write permission for others
poridhi@ubuntu:~/Desktop$ chmod o-w file.txt
chownChanges file or directory ownership.
Examples:
# Change ownership
poridhi@ubuntu:~/Desktop$ sudo chown user1:user1 file.txt
# Change ownership of a directory recursively
poridhi@ubuntu:~/Desktop$ sudo chown -R user1:user1 folder_name
df (Disk Free)Shows disk space usage of file systems.
Examples:
# Show disk space usage in human-readable format
poridhi@ubuntu:~$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 20G 30G 40% /
# Show disk space usage for a specific directory
poridhi@ubuntu:~$ df -h /home/poridhi
du (Disk Usage)Shows disk usage of files and directories.
Examples:
# Show disk usage of a directory
poridhi@ubuntu:~$ du -sh /home/poridhi
1.5G /home/poridhi
# Show disk usage of all files and directories
poridhi@ubuntu:~$ du -h --max-depth=1
freeDisplays memory usage.
Examples:
# Show memory usage in human-readable format
poridhi@ubuntu:~$ free -h
total used free shared buff/cache available
Mem: 8.0G 2.5G 4.0G 200M 1.5G 5.2G
Swap: 2.0G 0B 2.0G